﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Reports.Cinema
{
    public class Report2 : DependencyObject
    {
        public static DependencyProperty StartDateProperty = DependencyProperty.Register("StartDate", typeof(DateTime), typeof(Report2));
        public DateTime StartDate
        {
            get { return (DateTime)GetValue(StartDateProperty); }
            set
            {
                SetValue(StartDateProperty, value);
            }
        }
        public static DependencyProperty EndDateProperty = DependencyProperty.Register("EndDate", typeof(DateTime), typeof(Report2));
        public DateTime EndDate
        {
            get { return (DateTime)GetValue(EndDateProperty); }
            set
            {
                SetValue(EndDateProperty, value);
            }
        }
        public Report2()
        {
            EndDate = DateTime.Now;
            if (DateTime.Now.Month > 1)
                StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month - 1, DateTime.Now.Day);
            else StartDate = new DateTime(DateTime.Now.Year - 1, 12, DateTime.Now.Day);
        }
        public CrystalReport2 Load(TinyCinema.Bussiness.Login.Employee LoginInfo)
        {
            DateTime StartDate1 = new DateTime(StartDate.Year, StartDate.Month, 1);
            DateTime EndDate1 = new DateTime(EndDate.Year,EndDate.Month,1);
            if (EndDate.Month == 12) EndDate1 = new DateTime(EndDate.Year+1, 1, 1);
            string sql = "select table3.Thang,table3.Nam, case TongVe when null then 0 else TongVe end as TongVe, TongTD, (TongTD+TongVe) as Tong from  (select table1.Thang,table1.Nam, case TongTD when null then 0 else TongTD end as TongTD from   ( select MONTH(ngayHDTD) as Thang,YEAR(NgayHDTD) as Nam from HOADONTHUCDON "
                    + " where NgayHDTD>='" + StartDate + "'"
                    + " and NgayHDTD<='" + EndDate1 + "'"
                    + " group by MONTH(ngayHDTD),YEAR(ngayhdtd) union  select MONTH(ngayHDV) as Thang,Year(ngayHDV) as Nam from HOADONVE "
                    + " where NgayHDV>='" + StartDate + "'"
                    + " and NgayHDV<='" + EndDate1 + "'"
                    + " group by MONTH(ngayHDV),year(ngayhdv) ) as table1 left join  (select MONTH(ngayHDTD) as Thang,YEAR(NgayHDTD) as Nam,sum(TriGia) as TongTD from HOADONTHUCDON group by MONTH(ngayHDTD),year(ngayhdtd)) as table2 on table1.Thang = table2.Thang and table1.Nam = table2.Nam)  as table3 left join (select MONTH(ngayHDV) as Thang,Year(ngayHDV) as Nam,sum(TriGia) as TongVe from HOADONVE group by MONTH(ngayHDV),year(ngayHDV)) as table4 on table3.Thang = table4.Thang and table4.Nam = table3.Nam ";
            CrystalReport2 report = new CrystalReport2();
            report.SetDataSource(TinyCinema.Data.Execution.Excute(sql));
            report.SetParameterValue("StartDate", StartDate);
            report.SetParameterValue("EndDate", EndDate);
            report.SetParameterValue("EmployeeName", LoginInfo.Emp.Name);
            return report;
        }
    }
}
